const app = getApp();
import { request} from '../../utils/request';
import config from '../../utils/config'
Page({

    /**
     * 页面的初始数据
     */
    data: {
        custom:wx.getMenuButtonBoundingClientRect(),
        picUrl:config.picUrl,
        formData: {album_id: 1},
        provinceList:[{code:'',name:'请选择'}],
        provinceIndex:0,
        cityList:[{code:'',name:'请选择'}],
        cityIndex:0,
        countyList:[{code:'',name:'请选择'}],
        countyIndex:0,
        settleProvinceList:[{code:'',name:'请选择'}],
        settleProvinceIndex:0,
        settleCityList:[{code:'',name:'请选择'}],
        settleCityIndex:0,
        accountTypeList:[{code:'57',name:'公司基本存款账户'},{code:'58',name:'自然人结算账户'}],
        accountTypeIndex:0,
        btnTxt:'获取验证码',
        zuobiaoTxt:'点击获取',
        disabled:false,
        user:{},
        email:'',
        merRegName:'',
        merAddr:'',
        provinceCode:'',
        province:'',
        cityCode:'',
        city:'',
        countyCode:'',
        county:'',
        latitude:'',
        longtude:'',
        licenseName:'',
        licenseNo:'',
        licenseDtStart:'',
        licenseDtEnd:'',
        businessContent:'',
        larName:'',
        larIdCard:'',
        larIdCardStart:'',
        larIdCardEnd:'',
        contactMobile:'',
        settleProvinceCode:'',
        settleProvinceName:'',
        settleCityCode:'',
        settleCityName:'',
        openningBankCode:'',
        openningBankName:'选择开户行',
        clearingBankCode:'',
        accountType:'57',
        accountName:'',
        accountNo:'',
        id_card_front_url:'',//身份证人像面
        id_card_front:'',
        id_card_behind_url:'',//身份证国徽面
        id_card_behind:'',
        business_licence_url:'',//营业执照照片
        business_licence:'',
        bank_card_url:'',//银行卡照片
        bank_card:'',
        // agree_ment_url:'',//协议
        // agree_ment:'',//协议
        opening_permit_url:'',//公户开户许可证
        opening_permit:'',
        checkstand_img_url:'',//收银台照片
        checkstand_img:'',
        shop_outside_img_url:'',//门头照片
        shop_outside_img:'',
        shop_inside_img_url:'',//公司内部照片
        shop_inside_img:'',
        username:'',
        password:'',
        re_password:'',
        captcha:'',
        brand_avatar:'',
        story:'',
        extension:'',
        authentication_type:3,
        agree:false
    },
    onLoad(options) {
        // this.getRegion(1,'provinceList')
        this.getSettleRegion(1,'settleProvinceList')
        var user = wx.getStorageSync('userInfo')
        this.setData({user})
    },
    getRegion(code,field){
        wx.showLoading()
        console.log(1)
        request({
            url:"/miniprogram/lklpay/getRegion",
            data:{code},
            type:'get'
        }).then(res=>{
            console.log(2)
            wx.hideLoading()
            this.setData({[field]:res})
        })
    },
    cityPickerChange(e){
        var {field,index,list,name} = e.currentTarget.dataset
        this.setData({
            [index]:e.detail.value,
            [field]:this.data[list][e.detail.value].code
        })
        if(name){
            this.setData({[name]:this.data[list][e.detail.value].name})
        }
        if(field == 'provinceCode'){
            this.getRegion(this.data[list][e.detail.value].code,'cityList')
        }else if(field == 'cityCode'){
            this.getRegion(this.data[list][e.detail.value].code,'countyList')
        }else if(field == 'settleProvinceCode'){
            this.getSettleRegion(this.data[list][e.detail.value].code,'settleCityList')
        }
    },
    getSettleRegion(code,field){
        wx.showLoading()
        request({
            url:"/miniprogram/lklpay/getBankAddress",
            data:{code},
            type:'get'
        }).then(res=>{
            wx.hideLoading()
            this.setData({[field]:res})
        })
    },
    getAddress(){
        if(this.data.zuobiaoTxt == '已获取'){
            config.toast('已获取完成，无需重复获取。',201,()=>{})
            return false
        }
        wx.showLoading()
        wx.getFuzzyLocation({
            type: 'wgs84',
            success: res => {
                wx.hideLoading()
                if (res.errMsg != 'getFuzzyLocation:ok') {
                    wx.showToast({title: res.errMsg, icon: 'none'})
                    return false
                }
                this.setData({
                    latitude:res.latitude,
                    longtude:res.longitude,
                    zuobiaoTxt:'已获取'
                })
            }
        })
    },
    naviGetBank(){
        if(!this.data.settleCityCode){
            config.toast('请先选择开户行所在城市',201,()=>{})
            return false
        }
        wx.navigateTo({url:'/pages/get_bank/get_bank?code='+this.data.settleCityCode})
    },
    uploadImg(e) {
        var field = e.currentTarget.dataset.field
        if('id' in e.currentTarget.dataset){
            var id = e.currentTarget.dataset.id
            this.upload(1,{imgType:id.toUpperCase()}, res => {
                if (res.code != 200) {
                    wx.showToast({title: res.msg, icon: 'none'})
                    return false
                }
                this.setData({
                    [field]: res.data.src,
                    [id]:res.data.url
                })
            })
        }else{
            this.upload(1,{imgType:'none'}, res => {
                console.log(e)
                console.log(res)
                if (res.code != 200) {
                    wx.showToast({title: res.msg, icon: 'none'})
                    return false
                }
                this.setData({[field]: res.data.src})
            })
        }
    },
    delImg(e) {
        this.setData({[e.currentTarget.dataset.field]: ''})
        if('id' in e.currentTarget.dataset){
            this.setData({[e.currentTarget.dataset.id]: ''})
        }
    },
    upload(count,formData, callback) {
        wx.showLoading()
        wx.chooseImage({
            count: count,
            sizeType: ['compressed', 'original'],
            success: res => {
                for (let v of res.tempFiles) {
                    wx.uploadFile({
                        url: config.url + '/miniprogram/lklpay/uploadFile',
                        filePath: v.path,
                        name: 'file',
                        formData: formData,
                        success: res => {
                            wx.hideLoading()
                            callback(JSON.parse(res.data))
                        }
                    })
                }
            },
            fail: err => {
                wx.hideLoading()
            }
        })
    },
    submit(e){
        if(!this.data.agree){
            config.toast('请先阅读并同意《入驻协议》。',201,()=>{})
            return false
        }
        var user = this.data.user
        var {settleProvinceCode,settleProvinceName,settleCityCode,settleCityName,openningBankCode,openningBankName,clearingBankCode,accountType,accountName,bank_card_url,bank_card,opening_permit_url,opening_permit,username,password,re_password,email} = this.data
        if(accountType == '57' && (!opening_permit)) {
            config.toast("当结算账户类型为对公时，开户许可证，收银台照片，门头照，内部照片必须上传！",201,()=>{})
            return false
        }else if(accountType == '58' && (!bank_card)){
            config.toast("当结算账户类型为对私时，银行卡照片必须上传！",201,()=>{})
            return false
        }
        if(!settleProvinceCode || !settleProvinceName || !settleCityCode || !settleCityName || !openningBankCode || !openningBankName || !clearingBankCode || !accountType || !accountName || !email){
            config.toast("上述所有选项均为必填或必传项，否则将导致审核失败！",201,()=>{})
            return false
        }
        if((user.authentication_type == 3) && (!username || !password || !re_password)){
            config.toast("发行商必需填写剧转后台账号密码。",201,()=>{})
            return false
        }
        var obj = {settleProvinceCode,settleProvinceName,settleCityCode,settleCityName,openningBankCode,openningBankName,clearingBankCode,accountType,accountName,bank_card_url,bank_card,opening_permit_url,opening_permit,username,password,re_password,email,user_id:this.data.user.id}
        this.registor(obj)
    },
    getCode(){
        var phone = this.data.contactMobile
        if(!phone){
            config.toast("请先填写手机号码后再获取验证码",201,res=>{})
            return false
        }
        this.setData({disabled:true})
        var num = 60;
        var timer = setInterval(()=>{
            if(num <= 1){
                this.setData({btnTxt:'获取验证码',disabled:false})
                clearInterval(timer)
            }else{
                num--;
                this.setData({btnTxt:num+"秒后重试"})
            }
        },1000)
        wx.showLoading()
        request({
            url:"/miniprogram/system/sendSmsCode",
            data:{openid:this.data.user.openid,phone:phone},
        }).then(res=>{
            wx.hideLoading()
            config.toast(res.msg,201,()=>{})
        })
    },
    changeValue(e){
        var field = e.currentTarget.dataset.field,
            value = e.detail.value
        this.setData({[field]:value})
    },
    registor(obj){
        var user = this.data.user
        wx.showLoading()
        request({
            url:'/miniprogram/lklpay/merchantRenzheng',
            data:{...obj},
            type:'post',
        }).then(res=>{
            wx.hideLoading()
            config.toast(res.msg,201,rel=>{

                if(res.code == 200){
                //     if(user.authentication_type == 3){
                //         wx.navigateTo({url: '/pages/member_recharge/member_recharge'})
                //     }else if(user.authentication_type == 4){
                //         wx.navigateTo({url: '/pages/member_recharge2/member_recharge'})
                //     }
                    setTimeout(()=>{wx.switchTab({url:"/pages/my/my"})},1500)
                }
            })
        })
    },
    downloadMoban(){
        wx.downloadFile({
            url: 'https://www.zgjzwl.com/static/file/dianqian.docx', //仅为示例，并非真实的资源
            success (res) {
                config.toast("预览文件后点击右上角...选择保存到手机或发送给好友。",201,()=>{
                    setTimeout(()=>{
                        const filePath = res.tempFilePath
                        wx.openDocument({
                            filePath: filePath,
                            showMenu:true, //关键点
                            success: function (res) {
                                console.log('打开文档成功')
                            }
                        })
                    },1500)
                })

            }
        })
    },
    radioChange(e){
        if(e.detail.value.length == 1){
            this.setData({agree:true})
        }else{
            this.setData({agree:false})
        }
    }
})